package com.sharekitchen.service.impl;

import com.sharekitchen.entity.Order;
import com.sharekitchen.entity.PieChart;
import com.sharekitchen.mapper.OrderMapper;
import com.sharekitchen.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Order> findAllOrder() {
        return orderMapper.findAllOrder();
    }

    @Override
    public boolean existOrderId(String orderId) {
        return orderMapper.existOrderId(orderId) != null;
    }

    @Override
    public boolean addOrder(Order order) {
        return orderMapper.addOrder(order) > 0;
    }

    @Override
    public Order findOrderById(String orderId) {
        return orderMapper.findOrderById(orderId);
    }

    @Override
    public boolean deleteOrderById(String orderId) {
        return orderMapper.deleteOrderById(orderId) > 0;
    }

    @Override
    public boolean updateOrder(Order order) {
        return orderMapper.updateOrder(order) > 0;
    }

    @Override
    public List<PieChart> getPieChartData() {
        List<Order> allOrder = orderMapper.findAllOrder();
        HashMap<String, PieChart> map = new HashMap<>();
        for (Order order : allOrder) {
            String orderHour = Integer.toString(Integer.parseInt(order.getOrderTime()) / 60 / 60) + "小时";
            if (map.get(orderHour) == null) {
                map.put(orderHour, new PieChart(1,orderHour));
            } else {
                map.put(orderHour, new PieChart(map.get(orderHour).getValue() + 1, orderHour));
            }
        }

        List<PieChart> pieCharts = new ArrayList<>(map.values());

        return pieCharts;
    }

}
